home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / EXGRAF3D / BOXES.C next >
C/C++ Source or Header  |  1990-09-27  |  5KB  |  226 lines

  1. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  2. /*                                                                               */
  3. /* Boxes.c    --      Translation of Pascal 'LISA/EXAMPLE/BOXES.TEXT'            */
  4. /*                    from Apple Programmer's and Developer's Association        */
  5. /*                    disk 'Macintosh Example Applications and Sources v.1.0'    */
  6. /*                                                                               */
  7. /*                    Translation to LightSpeed C by Lewis E. Garrett - 9/27/90  */
  8. /*                                                   CIS 71147,2202               */
  9. /*                                                                               */
  10. /*                                                                               */
  11. /*╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤ÑÑÑ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤*/
  12.  
  13. #include "Graf3D.h"
  14.  
  15. /* CONST */
  16. #define boxCount 15
  17. #define keyOrMouse mDownMask+keyDownMask
  18. #define NIL 0L
  19.  
  20.  
  21. typedef struct Box3D{
  22.     Point3D pt1;
  23.     Point3D pt2;
  24. };
  25.  
  26. WindowPtr w;
  27.  
  28. /*    GrafPort myPort;    */
  29. Port3D        gPort3D;
  30. struct        Box3D boxArray[15];
  31. int         nBoxes;
  32. int         i;
  33. EventRecord dummy;
  34.  
  35. Port3D        *ptr3D;
  36.  
  37. main()
  38.  
  39. { /* main program */
  40.     FlushEvents(everyEvent, 0);
  41.     InitGraf(&thePort);
  42.     ptr3D    = &gPort3D;
  43.     InitGrf3D(&ptr3D);
  44.     InitFonts();
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs(NIL);
  49.  
  50.     InitCursor();
  51.     HideCursor();
  52.  
  53.     w = NewWindow(NIL, &screenBits.bounds, "\p", TRUE, 2, (WindowPtr)(-1L), FALSE, 0L);
  54.     SetPort(w);
  55.  
  56.  
  57.     Draw3D();
  58.  
  59.     DisposeWindow(w);
  60.  
  61. }
  62.  
  63. Draw3D()
  64. {
  65.     Rect inRect;
  66.     long n;
  67.  
  68.     Open3DPort(&gPort3D);
  69.     ViewPort(&thePort->portRect); /* put the image in this rect */
  70.     LookAt(FixRatio(-100, 1), FixRatio(75, 1), FixRatio(100, 1),
  71.     FixRatio(-75, 1)); /* aim the camera into 3D space */
  72.     ViewAngle(FixRatio(30, 1)); /* choose lens focal length */
  73.     Identity();
  74.     Roll(FixRatio(20, 1));
  75.     Pitch(FixRatio(70, 1)); /* roll and pitch the plane */
  76.  
  77.     while (! OSEventAvail(keyOrMouse, &dummy))
  78.     {
  79.         nBoxes = 0;
  80.  
  81.         do
  82.         {
  83.             MakeBox();
  84.         }
  85.         while (nBoxes<boxCount);
  86.  
  87.         PenPat(white);
  88.         BackPat(black);
  89.         EraseRect(&thePort->portRect);
  90.  
  91.         for ( i = -10; i<=10; i++ )
  92.         {
  93.             MoveTo3D(FixRatio(i*10, 1), FixRatio(-100, 1), 0);
  94.             LineTo3D(FixRatio(i*10, 1), FixRatio(100, 1), 0);
  95.         }
  96.  
  97.         for ( i = -10; i<=10; i++ )
  98.         {
  99.             MoveTo3D(FixRatio(-100, 1), FixRatio(i*10, 1), 0);
  100.             LineTo3D(FixRatio(100, 1), FixRatio(i*10, 1), 0);
  101.         }
  102.  
  103.         for ( i = nBoxes-1; i>=0; i-- )
  104.         {
  105.             DrawBrick(boxArray[i].pt1,boxArray[i].pt2);
  106.         }
  107.  
  108.         for ( n = 1; n<=1000000; n++ )
  109.         {
  110.             n = n;
  111.         }
  112.  
  113.     }
  114.  
  115.     ShowCursor();
  116.  
  117. }
  118.  
  119. DrawBrick(pt1,pt2)
  120. Point3D pt1,pt2;
  121.  
  122. {
  123.     RgnHandle tempRgn;
  124.  
  125. /*    BackColor(Random());        enable for randomly colored boxes */
  126.     tempRgn = NewRgn();
  127.     OpenRgn();
  128.     MoveTo3D(pt1.x, pt1.y, pt1.z); /* front face, y=y1 */
  129.     LineTo3D(pt1.x, pt1.y, pt2.z);
  130.     LineTo3D(pt2.x, pt1.y, pt2.z);
  131.     LineTo3D(pt2.x, pt1.y, pt1.z);
  132.     LineTo3D(pt1.x, pt1.y, pt1.z);
  133.     CloseRgn(tempRgn);
  134.     FillRgn(tempRgn, white);
  135.  
  136.     OpenRgn();
  137.     MoveTo3D(pt1.x, pt1.y, pt2.z); /* top face, z=z2 */
  138.     LineTo3D(pt1.x, pt2.y, pt2.z);
  139.     LineTo3D(pt2.x, pt2.y, pt2.z);
  140.     LineTo3D(pt2.x, pt1.y, pt2.z);
  141.     LineTo3D(pt1.x, pt1.y, pt2.z);
  142.     CloseRgn(tempRgn);
  143.     FillRgn(tempRgn, gray);
  144.  
  145.     OpenRgn();
  146.     MoveTo3D(pt2.x, pt1.y, pt1.z); /* right face, x=x2 */
  147.     LineTo3D(pt2.x, pt1.y, pt2.z);
  148.     LineTo3D(pt2.x, pt2.y, pt2.z);
  149.     LineTo3D(pt2.x, pt2.y, pt1.z);
  150.     LineTo3D(pt2.x, pt1.y, pt1.z);
  151.     CloseRgn(tempRgn);
  152.     FillRgn(tempRgn, black);
  153.  
  154.     PenPat(white);
  155.     MoveTo3D(pt2.x, pt2.y, pt2.z); /* outline right */
  156.     LineTo3D(pt2.x, pt2.y, pt1.z);
  157.     LineTo3D(pt2.x, pt1.y, pt1.z);
  158.     PenNormal();
  159.  
  160.     DisposeRgn(tempRgn);
  161. }
  162.  
  163. MakeBox()
  164.  
  165. {
  166.     struct Box3D myBox;
  167.     int i, j, h, v;
  168.     Point3D p1, p2;
  169.     Rect myRect;
  170.     Rect testRect;
  171.     short sw;
  172.  
  173.     p1.x = FixRatio((Random() % 70 - 15), 1);
  174.     p1.y = FixRatio((Random() % 70 - 10), 1);
  175.  
  176.     p1.z = 0;
  177.  
  178.     p2.x = p1.x + FixRatio((10 + abs(Random()) % 30), 1);
  179.     p2.y = p1.y + FixRatio((10 + abs(Random()) % 45), 1);
  180.  
  181.     p2.z = p1.z + FixRatio((10 + abs(Random()) % 35), 1);
  182.  
  183.     /* reject box if it intersects one already in list */
  184.     SetRect(&myRect, HiWord(p1.x), HiWord(p1.y), HiWord(p2.x), HiWord(p2.y));
  185.  
  186.     i=0;
  187.     do
  188.     {
  189.         sw = 0;             /* clear exit switch */
  190.         SetRect(&testRect, HiWord(boxArray[i].pt1.x), HiWord(boxArray[i].pt1.y),
  191.         HiWord(boxArray[i].pt2.x), HiWord(boxArray[i].pt2.y));
  192.         InsetRect(&testRect, -1, -1);
  193.         if ( SectRect(&myRect, &testRect, &testRect) )
  194.             break; /* EXIT(MakeBox); */
  195.         sw = 1;
  196.         i=i+1;
  197.     }
  198.     while (i<(nBoxes-1));
  199.  
  200.     if (sw==1)                /* sw=1 means previous loop finished */
  201.     {
  202.         myBox.pt1 = p1;
  203.         myBox.pt2 = p2;
  204.  
  205.         /* calc midpoint of box and its distance from the eye */
  206.  
  207.         i = 0;
  208.         boxArray[nBoxes].pt1 = myBox.pt1; /* sentinel */
  209.         boxArray[nBoxes].pt2 = myBox.pt2;
  210.  
  211.         while (
  212.         ((myBox.pt1.y > boxArray[i].pt2.y) && (myBox.pt2.y > boxArray[i].pt1.y)) ||
  213.             ((myBox.pt1.x < boxArray[i].pt2.x) && (myBox.pt2.x < boxArray[i].pt1.x)) )
  214.             i = i + 1;        /* insert in order of dist */
  215.  
  216.         for ( j = nBoxes; j>=(i+1); j-- )
  217.         {
  218.             boxArray[j] = boxArray[j - 1];
  219.         }
  220.         boxArray[i] = myBox;
  221.         nBoxes = nBoxes + 1;
  222.     }
  223. }
  224.  
  225.  
  226.